//Page Object
var api = require("../../api.js"),
    r = "",
    app = getApp();
Page({
    data: {
        returnData: [],
        actEndTimeList: [],
        countDownList: []
    },
    onLoad: function(options) {
        r = this;
        r.setData({
            actUUID: options.actUUID
        });
    },
    onShow() {
        r.initData();
    },
    timeFormat(param) { //小于10的格式化函数
        return param < 10 ? '0' + param : param;
    },
    countDown: function() { //倒计时函数
        var obj = null;
        app.globalData.time = setTimeout(() => {
            // 获取当前时间，同时得到活动结束时间数组
            let newTime = new Date().getTime();
            let endTimeList = this.data.returnData.endTime.replace(/\-/g, "/");

            // 对结束时间进行处理渲染到页面
            // endTimeList.forEach(o => {
            let endTime = new Date(endTimeList).getTime();
            let timeMax = Number(endTime) - Number(newTime);
            // 如果活动未结束，对时间进行处理
            if (timeMax > 0) {
                let time = (endTime - newTime) / 1000;
                // 获取天、时、分、秒
                let day = parseInt(time / (60 * 60 * 24));
                let hou = parseInt(time % (60 * 60 * 24) / 3600);
                let min = parseInt(time % (60 * 60 * 24) % 3600 / 60);
                let sec = parseInt(time % (60 * 60 * 24) % 3600 % 60);
                obj = {
                    day: this.timeFormat(day),
                    hou: this.timeFormat(hou),
                    min: this.timeFormat(min),
                    sec: this.timeFormat(sec)
                }
                r.countDown();
            } else { //活动已结束，全部设置为'00'
                obj = {
                    day: '00',
                    hou: '00',
                    min: '00',
                    sec: '00'
                }
            }
            // 渲染，然后每隔一秒执行一次倒计时函数
            r.setData({
                time: obj
            });
        }, 1000);
    },
    onShareAppMessage() {
        var that = this
        var openid = getApp().globalData.wxUserInfo.openId;
        let url = `/pages/index/index?inviteOpenId=${openid}&id=${that.data.returnData.actId}&inviteType=2&&shareType=7`;
        return {
            title: "快来和我一起拼团吧",
            path: url,
            imageUrl: that.data.returnData.actImg
        };
    },

    initData: function() {
        var t = new Object();
        t.openId = getApp().globalData.wxUserInfo.openId;
        t.actUUID = r.data.actUUID
        app.getData.assDetail(t).then(res => {
            var t = res.data;
            var images = t.avatarUrl.split(',');
            r.setData({
                images: images,
                returnData: t,
            });
            r.countDown();
        }).catch(res => {
            console.log(res)
        })


    },
    mine: function() {
        var pages = getCurrentPages(); //获取页面栈
        if (pages.length > 1) {
            //上一个页面实例对象
            var prePage = pages[pages.length - 2];
            //调用上一个页面的onShow方法
            prePage.init();
            wx.navigateBack({
                delta: 1
            })
        }
    }

});